﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Nokia_Portal_WPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            textbox1.Visibility = Visibility.Hidden;
            apply_btn.Visibility = Visibility.Hidden;

            expander1.Header = "8 - Enter " + LocalIPAddress() + " as the APN and 8888 as the Port";
            textbloxk1.Text = "<- Type " + LocalIPAddress() + " here";
            expander2.Header = "9 - Go to http://" + LocalIPAddress() + ":8888/FiddlerRoot.cer";
        }

        public string LocalIPAddress()
        {
            IPHostEntry host;
            string localIP = "";
            host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    localIP = ip.ToString();
                    break;
                }
            }
            return localIP;
        }

        private void installfiddler_btn_Click(object sender, RoutedEventArgs e)
        {
            //Installs Fiddler2
            ProcessStartInfo start = new ProcessStartInfo();
            start.FileName = @".\Fiddler2Setup.exe";
            using (Process proc = Process.Start(start))
            {
                proc.WaitForExit();

                if(proc.ExitCode == 0)
                {
                    MessageBox.Show("Installation was successful", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
                }
                else
                {
                    MessageBox.Show("Installation was not successful\r\nError code: " + proc.ExitCode.ToString(), "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }

        private void launchfiddler_btn_Click(object sender, RoutedEventArgs e)
        {
            //Creates the script that Fiddler uses
            try
            {
                System.IO.Directory.CreateDirectory(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Fiddler2");
                System.IO.Directory.CreateDirectory(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Fiddler2\Scripts");
                File.Create(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Fiddler2\Scripts\CustomRules.js").Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }

            //Opens the sample script for editing
            StreamReader reader = new StreamReader(@".\SampleRules.js");
            string fileContent = reader.ReadToEnd();
            reader.Close();
           
            //Replaces the temp strings with user choices
            fileContent = fileContent.Replace("*;TEMPAPPS;*", basecomp_cb.SelectedItem.ToString().Replace("System.Windows.Controls.ComboBoxItem: ", ""));
            if (phonecomp_cb.SelectedItem.ToString().Replace("System.Windows.Controls.ComboBoxItem: ", "") == "Other...")
            {
                fileContent = fileContent.Replace("*;TEMPDEVICE;*", textbox1.Text);
            }
            else
            {
                fileContent = fileContent.Replace("*;TEMPDEVICE;*", phonecomp_cb.SelectedItem.ToString().Replace("System.Windows.Controls.ComboBoxItem: ", ""));
            }

            //Applies changes to the script
            StreamWriter writer = new StreamWriter(File.OpenWrite(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Fiddler2\Scripts\CustomRules.js"));
            writer.Write(fileContent);
            writer.Close();

            //Starts Fiddler
            ProcessStartInfo start = new ProcessStartInfo();
            start.FileName = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles) + @"\Fiddler2\Fiddler.exe";
            using (Process proc = Process.Start(start)) { }
        }

        private void watchvid_btn_Click(object sender, RoutedEventArgs e)
        {
            Video vid = new Video();
            vid.Show();
        }

        private void expanded()
        {
            firstinstructions.Visibility = Visibility.Hidden;
        }

        private void collapsed()
        {
            firstinstructions.Visibility = Visibility.Visible;
        }

        private void phonecomp_cb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (phonecomp_cb.SelectedItem.ToString().Replace("System.Windows.Controls.ComboBoxItem: ","") == "Other...")
            {
                textbox1.Visibility = Visibility.Visible;
                apply_btn.Visibility = Visibility.Visible;
            }
        }

        private void Expander_Expanded(object sender, RoutedEventArgs e)
        {
            expanded();
        }

        private void Expander_Collapsed(object sender, RoutedEventArgs e)
        {
            collapsed();
        }

        private void Expander_Expanded_1(object sender, RoutedEventArgs e)
        {
            expanded();
        }

        private void Expander_Collapsed_1(object sender, RoutedEventArgs e)
        {
            collapsed();
        }

        private void Expander_Expanded_2(object sender, RoutedEventArgs e)
        {
            expanded();
        }

        private void Expander_Collapsed_2(object sender, RoutedEventArgs e)
        {
            collapsed();
        }

        private void Expander_Expanded_3(object sender, RoutedEventArgs e)
        {
            expanded();
        }

        private void Expander_Collapsed_3(object sender, RoutedEventArgs e)
        {
            collapsed();
        }

        private void Expander_Expanded_4(object sender, RoutedEventArgs e)
        {
            expanded();
        }

        private void Expander_Collapsed_4(object sender, RoutedEventArgs e)
        {
            collapsed();
        }

        private void Expander_Expanded_5(object sender, RoutedEventArgs e)
        {
            expanded();
        }

        private void Expander_Collapsed_5(object sender, RoutedEventArgs e)
        {
            collapsed();
        }

        private void Expander_Expanded_6(object sender, RoutedEventArgs e)
        {
            expanded();
        }

        private void Expander_Collapsed_6(object sender, RoutedEventArgs e)
        {
            collapsed();
        }

        private void Expander_Expanded_7(object sender, RoutedEventArgs e)
        {
            expanded();
        }

        private void Expander_Collapsed_7(object sender, RoutedEventArgs e)
        {
            collapsed();
        }

        private void Expander_Expanded_8(object sender, RoutedEventArgs e)
        {
            expanded();
        }

        private void Expander_Collapsed_8(object sender, RoutedEventArgs e)
        {
            collapsed();
        }

        private void apply_btn_Click(object sender, RoutedEventArgs e)
        {
            textbox1.Visibility = Visibility.Hidden;
            apply_btn.Visibility = Visibility.Hidden;
        }

        private void basecomp_cb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            expander3.Header = "11 - Launch the Windows Store and search for " + basecomp_cb.SelectedItem.ToString().Replace("System.Windows.Controls.ComboBoxItem: ", "");
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Apps apps = new Apps();
            apps.Show();
        }
    }
}
